from django.shortcuts import render
from django_redis import get_redis_connection
import random
from django.http import JsonResponse,HttpResponse
# from utils.tencentcloud.msg import tencent_send_mes
from utils.aliyun.msg import Sample


def send_msm(request):
    # phone = request.POST.get('phone')  # 前端传来的手机号码
    phone = '18081208401'
    # 1、验证手机号码
    # 2、生成随机验证码
    code = random.randint(1000,9999)
    # 3、通过腾讯云或阿里云将验证码发送给指定手机
    # result = tencent_send_mes(phone,code)  #腾讯云
    result = Sample.aliyun_send_sms(phone,code)  #阿里云
    if not result: #发送失败
        return JsonResponse({'status': False, 'msg': '信息发送失败'})
    # 4、将手机号和验证码缓存到redis用于校验
    redis_conn = get_redis_connection()
    redis_conn.set(phone, code, ex=60)
    return JsonResponse({'status': True, 'msg': '信息发送成功'})